package utils.json;

import com.alibaba.fastjson.JSON;
import entity.ConfigFile;
import entity.LocalVideoConfig;
import entity.VideoItem;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import utils.push.PushUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 利用JSON文件对下载视频文件做持久化处理，存储下载文件的状态信息。
 * 提供获取、保存JSON信息方法。
 */
@Slf4j
public class VideoStateUtil {
    private static final ConfigFile config = InitConfig.getInstance().getConfig();
    private static LocalVideoConfig localVideoConfig;

    private static final VideoStateUtil instance = new VideoStateUtil();

    public static VideoStateUtil getInstance() {
        return instance;
    }

    public LocalVideoConfig getLocalVideoConfig() {
        return localVideoConfig;
    }

    private VideoStateUtil() {
        String localVideoConfigPath = config.getRSSHub().getLocalVideoConfigPath();
        try {
            String json = FileUtils.readFileToString(new File(localVideoConfigPath), StandardCharsets.UTF_8);
            if (!json.isEmpty()) {
                //配置文件不为空
                localVideoConfig = JSON.parseObject(json, LocalVideoConfig.class);
            } else {
                //配置文件为空
                localVideoConfig = new LocalVideoConfig();
                localVideoConfig.setDir(config.getYouGet().getDir());
                localVideoConfig.setVideos(new ArrayList<>());
            }
        } catch (IOException e) {
            //配置文件不存在情况
            log.info("本地配置文件不存在，创建配置文件中。。");
            try {
                localVideoConfig = new LocalVideoConfig();
                localVideoConfig.setDir(config.getYouGet().getDir());
                localVideoConfig.setVideos(new ArrayList<>());
                FileUtils.writeStringToFile(new File(localVideoConfigPath), "", StandardCharsets.UTF_8);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 保存配置文件
     */
    public static void saveConfigToFile() {
        try {
            log.info("LocalVideoConfig：开始保存。");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            localVideoConfig.setSaveTime(sdf.format(new Date()));
            FileUtils.writeStringToFile(new File(config.getRSSHub().getLocalVideoConfigPath()), JSON.toJSONString(localVideoConfig), StandardCharsets.UTF_8);
            log.info("LocalVideoConfig：保存成功。");
            if (config.getPush() != null && config.getPush().isEnable()) {
                log.info("PushPlus：开始推送。");
                int status = PushUtil.getInstance().PushPlus("本地配置文件状态", localVideoConfig.toJSON().toJSONString());
                if (status == 200) {
                    log.info("PushPlus：推送成功。");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断本地配置文件内VideoItem列表是否下载完毕
     */
    public void parseLocalVideoConfigFileVideoList() {
        List<VideoItem> localVideoConfigVideos = localVideoConfig.getVideos();
        for (VideoItem item : localVideoConfigVideos) {
            if (isEmptyVideo(item.getTitle())) {
                item.setIsDownload(2);
            } else {
                item.setIsDownload(0);
            }
        }
    }

    /**
     * 判断本地文件是否存在
     *
     * @param fileName 文件名，不带后缀名
     * @return 是否存在
     */
    private static boolean isEmptyVideo(String fileName) {
        File file = new File(localVideoConfig.getDir() + fileName + ".flv");
        File file1 = new File(localVideoConfig.getDir() + fileName + ".mp4");
        return file.exists() || file1.exists();
    }
}
